package com.tech.dcyao.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.util.EntityUtils;


import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

public class HttpClientWrapper {
    private CloseableHttpClient httpClient;
    private String baseUrl = "http://127.0.0.1:7001/respberry/";
    public HttpClientWrapper() {
        this.httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());// HttpClients.createDefault();
    }
    // 传入BaseUrl
    public void setBaseUrl(String url) {
        this.baseUrl = url;
    }
    public String getBaseUrl() {
        return this.baseUrl;
    }
    public String sendPostRequest(String pathUrl, String requestBody) throws IOException {
        String responseString = "";
        try {
            String url = baseUrl + pathUrl;
            System.out.println("url"+ url);
            // 创建 HttpClient 实例
//            CloseableHttpClient httpClient = HttpClients.createDefault();
            // 创建 POST 请求实例
            HttpPost httpPost = new HttpPost(url);
            // 设置请求内容
            StringEntity entity = new StringEntity(requestBody, "UTF-8");
            httpPost.setEntity(entity);
            httpPost.setHeader("Content-Type", "application/json");

            // 设置请求配置，包括超时时间
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(5000) // 设置 socket 超时时间，单位为毫秒
                    .setConnectTimeout(5000) // 设置连接超时时间，单位为毫秒
                    .build();
            httpPost.setConfig(requestConfig);

            // 执行请求
            CloseableHttpResponse response = this.httpClient.execute(httpPost);
            // 获取响应实体
            HttpEntity responseEntity = response.getEntity();
            // 打印响应状态
            System.out.println("响应状态: " + response.getStatusLine().getStatusCode());
            // 如果响应实体不为空，则转换为字符串并打印
            if (responseEntity != null) {
                responseString = EntityUtils.toString(responseEntity, "UTF-8");
                System.out.println("响应结果: " + responseString);

            }
            // 关闭 HttpClient
            response.close();
            // httpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return responseString;
    }


    public String getRequest(String url) throws IOException{

        // 构建get请求

        HttpGet get = new HttpGet(url);
        // 创建客户端


        try {

            // 客户端执行请求，获取响应

            HttpResponse response = this.httpClient.execute(get);

            // 获取响应的页面内容

            InputStream in = response.getEntity().getContent();

            StringBuilder sb = new StringBuilder();

            byte[]b = new byte[102400];

            int length;

            while ((length = in.read(b)) != -1) {

                sb.append(new String(b, 0, length, "utf-8"));

            }

            // 返回页面内容

            return sb.toString();

        } catch (Exception e) {

            e.printStackTrace();

            return null;

        }
    }
}